#include "cimageicon.h"

#include <buola/gl/cgltexture.h>
#include <buola/image/cimage.h>

namespace buola { namespace bdesktop {

CImageIcon::CImageIcon(PImage pImage)
    :   mImage(pImage),
        mTexture(nullptr)
{
}

CImageIcon::~CImageIcon()
{
}

double CImageIcon::GetRelativeWidth()
{
    return 1.0;
}

void CImageIcon::Draw(const CRect &pRect,double pOpacity)
{
    CGLTexture *lTexture=GetTexture();

    lTexture->Enable(false,true);

    double lTexArray[8]={0.0,0.0,1.0,0.0,1.0,1.0,0.0,1.0};
    double lVertexArray[8]={pRect.l,pRect.t,pRect.r,pRect.t,
                            pRect.r,pRect.b,pRect.l,pRect.b};

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glVertexPointer(2,GL_DOUBLE,0,lVertexArray);
    glTexCoordPointer(2,GL_DOUBLE,0,lTexArray);

    glColor4d(1.0,1.0,1.0,pOpacity);
    glEnable(GL_BLEND);
    glDrawArrays(GL_QUADS,0,4);

    lTexture->Disable();
}

CGLTexture *CImageIcon::GetTexture()
{
    if(!mTexture)
    {
        mTexture=new CGLTexture(CGLTexture::FLAG_AUTOMIPMAPS);
        mTexture->Bind(mImage);
    }

    return mTexture;
}

/*namespace bdesktop*/ } /*namespace buola*/ }
